/*
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 */

package spidex.table;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;

/**
 *
 * @author xatrix
 */
public class ImageResizer {
    private static final ImageResizer RESIZER = new ImageResizer();
    
    private ImageResizer() {
        
    }
    
    public static ImageResizer getInstance() {
        return RESIZER;
    }
    
    public ImageIcon resize(ImageIcon original) {
        if(original == null) {
            throw new NullPointerException("original");
        }
        int width =
            Integer.parseInt( ResourceBundle.getBundle("spidex/properties").
                getString("posterSmallWidth")
        );
        int height =
            Integer.parseInt( ResourceBundle.getBundle("spidex/properties").
                getString("posterSmallHeight")
        );
        if(width > original.getIconWidth() || height > original.getIconHeight())
            throw new IllegalArgumentException("Image is too small!");
                
        BufferedImage buffImg = new BufferedImage(
            width,height,BufferedImage.TYPE_INT_RGB
        );
        
        Graphics2D imageGraphics = buffImg.createGraphics();
        imageGraphics.drawImage(original.getImage(), 0, 0, width, height, null);
        return new ImageIcon( buffImg );
    }
}
